package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientType;
import ywpt.model.CostType;
import ywpt.web.common.PublicFunction;

public class CostTypeServiceImpl  extends HibernateEntityDao<CostType> implements CostTypeService{
	public CostTypeServiceImpl(){
		
	}

	@Override
	public String CreateTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(CostType.class, "costType");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<CostType> costTypes = this.getCostTypes(query);
		if ( costTypes.iterator().hasNext() )	
		{
			code = costTypes.iterator().next().getCode();
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return typeCode;
	}

	@Override
	public CostType getCostTypeById(Long id) {
		return this.get(id);
	}

	@Override
	public int getCostTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CostType> getCostTypes(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<CostType> getCostTypes() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CostType> getCostTypesByTypeName(String type, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientType.class, "costType");
		//query.add(Restrictions.eq("organType", "01"));
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%"),  
							          Restrictions.like("code", "%" + type + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("code"));
		List<CostType> costTypeList = new ArrayList<CostType>();
		List<CostType> costTypeLists = new ArrayList<CostType>();
		CostType costType =new CostType();
		
		costTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<CostType> itr = costTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			costType = itr.next();
			String ss = PublicFunction.format(costType.getCode() + "", 14, "  ", 1)
			          + costType.getType(); 
			
			costType.setType(ss);
			costTypeLists.add(costType);
		}
		return costTypeLists;
	}

	@Override
	public int getCostTypesCountByName(String type) {
		DetachedCriteria query = DetachedCriteria.forClass(CostType.class, "costType");
		if ( type != null && !type.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("type", "%" + type + "%" ),  
							          Restrictions.like("code", "%" + type + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public void removeCostType(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveCostType(CostType costType) {
		super.save(costType);
	}

	@Override
	public void updateCostType(CostType costType) {
		super.update(costType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CostType> getCostTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
}
